package com.thermal.thermal_inspection.controller;

import com.thermal.thermal_inspection.common.CommonResponse;
import com.thermal.thermal_inspection.entity.User;
import com.thermal.thermal_inspection.service.AccountService;
import com.thermal.thermal_inspection.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users/account")
public class AccountController {

    @Autowired
    AccountService accountService;

    /**
     * 登录
     */
    @PostMapping("/login")
    public CommonResponse<User> login(@RequestParam String username, @RequestParam String password){
        System.out.println("进入登录接口！");
        User loginRespVO = accountService.login(username, password);
        if (loginRespVO != null)
            return CommonResponse.createForSuccess("SUCCESS", loginRespVO);
        return CommonResponse.createForError("账号名或密码错误");
    }

    /**
     * 注册
     */
    @PostMapping("/register")
    public CommonResponse<Object> register(@RequestBody User user){
        int rows = accountService.register(user);
        if(rows == 0)
            return CommonResponse.createForError("注册失败");
        return CommonResponse.createForSuccess("SUCCESS", null);
    }

    /**
     * 修改信息
     */
    @PutMapping("/fixAccount")
    public CommonResponse<Object> fixAccount(@RequestBody User user){
        int rows = accountService.fixAccount(user);
        if(rows == 0)
            return CommonResponse.createForError("修改信息失败");
        return CommonResponse.createForSuccess("SUCCESS", null);
    }

    /**
     * 查找个人信息
     */
    @GetMapping("/getUserInfo")
    public CommonResponse<User> getUserInfo(int userId){
        User userInfo = accountService.getUserInfo(userId);
        if(userInfo == null)
            return CommonResponse.createForError("查找个人信息失败");
        return CommonResponse.createForSuccess("SUCCESS", userInfo);
    }
}
